package com.geetour.columbus.region.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.geetour.columbus.common.model.R.AreaPage;
import com.geetour.columbus.common.model.R.CityPage;
import com.geetour.columbus.common.model.R.ProvincePage;
import com.geetour.columbus.common.service.IAreaPageService;
import com.geetour.columbus.common.service.ICityPageService;
import com.geetour.columbus.common.service.IProvincePageService;
import com.geetour.columbus.common.utils.StringUtil;

@Controller
public class AreaPageController {

    private IProvincePageService provincePageServiceImpl;
    private ICityPageService     cityPageServiceImpl;
    private IAreaPageService     areaPageServiceImpl;

    @Autowired
    public void setProvincePageServiceImpl(IProvincePageService provincePageServiceImpl) {
        this.provincePageServiceImpl = provincePageServiceImpl;
    }

    @Autowired
    public void setCityPageServiceImpl(ICityPageService cityPageServiceImpl) {
        this.cityPageServiceImpl = cityPageServiceImpl;
    }

    @Autowired
    public void setAreaPageServiceImpl(IAreaPageService areaPageServiceImpl) {
        this.areaPageServiceImpl = areaPageServiceImpl;
    }

    @RequestMapping("/region/areaPageAddInit.jsp")
    public String addInit(ModelMap modelMap) {
        List<ProvincePage> provincePageList = provincePageServiceImpl.findAllProvincePages();
        modelMap.put("provincePageList", provincePageList);
        return "/region/areaPageAdd.html";
    }

    @RequestMapping("/region/areaPageAdd.jsp")
    public String addBatch(ModelMap modelMap, String province, String city, String areas) {
        List<String> names = StringUtil.lineInput(areas);
        if (province == null || city == null || names == null || names.isEmpty()) {
            return "/region/areaPageAdd.html";
        }
        try {
            int result = areaPageServiceImpl.insertAreaPageNames(province, city, names);
            List<ProvincePage> provincePageList = provincePageServiceImpl.findAllProvincePages();
            modelMap.put("result", result);
            modelMap.put("provincePageList", provincePageList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/region/areaPageAdd.html";
    }

    @RequestMapping("/region/areaPageQuery.jsp")
    public String query(ModelMap modelMap, AreaPage area) {
        if (area != null) {
            modelMap.put("area", area);
            List<AreaPage> areaPageList = areaPageServiceImpl.findAreaPages(area);
            modelMap.put("areaPageList", areaPageList);
            List<CityPage> cityPageList = cityPageServiceImpl.findCityPagesByProvince(area.getProvince());
            modelMap.put("cityPageList", cityPageList);
        }
        List<ProvincePage> provincePageList = provincePageServiceImpl.findAllProvincePages();
        modelMap.put("provincePageList", provincePageList);
        return "/region/areaPageList.html";
    }
    
    @RequestMapping("/region/areaPageDelete.jsp")
    public String delete(ModelMap modelMap, AreaPage area) {
        try{
            areaPageServiceImpl.deleteAreaPage(area.getId());
        }catch(Exception e){
            e.printStackTrace();
        }
        area.setId(null);
        return this.query(modelMap, area);
    }

}
